package com.education.teacher.controller;

import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.education.model.ExamMessageModel;
import com.education.model.ResultDo;
import com.education.service.QueryExamPage;
import com.github.pagehelper.PageInfo;

import javax.servlet.http.HttpSession;

/**
 * @author 考试信息表
 */
@RestController
@RequestMapping("/api/master")
public class ExamMessageController {
    /**
     * 日志记录类
     */
    private Logger logger = Logger.getLogger(ExamMessageController.class);
    /**
     * 调用服务类方法
     */
    @Reference
    private QueryExamPage qp;


    /**
     * @param pageSize 当前一页有多少数据
     * @param pageNo   当前有多少页
     * @return 分页集合
     * @throws Exception 返回错误
     * @throws Exception 向上抛出异常
     */
    @RequestMapping(path = "/queryExam", method = RequestMethod.GET)
    public ResultDo<PageInfo<ExamMessageModel>> queryExam(Integer pageNo, Integer pageSize,HttpSession session) throws Exception {

        int tid = Integer.parseInt((String) session.getAttribute("teacherId"));

        PageInfo<ExamMessageModel> pro = qp.getExamMessageBy(tid, pageNo, pageSize);

        return new ResultDo<>(pro);
    }

    /**
     * @param el 传进来增加的值
     * @throws Exception 抛出异常
     */
    @RequestMapping(path = "/addExam", method = RequestMethod.POST)
    public ResultDo<Void> addExam(@RequestBody ExamMessageModel el, HttpSession session) throws Exception {

        int tid = Integer.parseInt((String) session.getAttribute("teacherId"));

        Date now = new Date();
        el.setExamAddTime(now);
        el.setExamMotifyDate(now);
        el.setExamMotifyPerson(tid);
        el.setTeacherId(tid);
        qp.addexam(el);
        return new ResultDo<>();
    }

    /**
     * @param map 输入ID号
     * @return 返回是否成功
     * @throws Exception 向上抛出异常
     *                   废弃 已由cancelExam代替
     */
    @RequestMapping(path = "/delExam", method = RequestMethod.POST)
    public int deleExam(@RequestBody Map<String, Object> map) throws Exception {
        qp.deleup((Integer) map.get("examid"));
        return 1;
    }

    /**
     * 2017/8/6 增加 by  雷紫辉
     * 取消考试
     *
     * @param examId
     * @param cancelReason 用于发短信，不存入数据库
     * @return
     */
    @RequestMapping(path = "/cancelExam", method = RequestMethod.POST)
    public ResultDo<Void> cancelExam(Integer examId, String cancelReason, HttpSession session) {

        int tid = Integer.parseInt((String) session.getAttribute("teacherId"));
        ExamMessageModel el = new ExamMessageModel();
        Date now = new Date();
        el.setExamMotifyDate(now);
        el.setExamMotifyPerson(tid);
        el.setExamId(examId);
        int num = qp.cancelExam(el);
        ResultDo<Void> resultDo = new ResultDo<>();
        if (num == 0) {
            resultDo.setResCode(105);
        }
        return resultDo;
    }
}
